/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.servlet;

import com.sjtu.bean.UserBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author p w
 */
public class UserCheckListServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    public void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<UserBean> userList = UserBean.getUserList();
//        System.out.println(userList);
        ArrayList<Integer> idList = new ArrayList<Integer>();
        ArrayList<Integer> scoreList = new ArrayList<Integer>();
        ArrayList<String> nameList = new ArrayList<String>();
        ArrayList<Date> dateList = new ArrayList<Date>();
        ArrayList<String> departmentList = new ArrayList<String>();
        PrintWriter writer = response.getWriter();
        JSONObject jsonRes = new JSONObject();
        for (int i = 0; i < userList.size(); i++) {
            idList.add(userList.get(i).getId());
            scoreList.add(userList.get(i).getScore());
            nameList.add(userList.get(i).getName());
            dateList.add(userList.get(i).getCreateDate());
            departmentList.add(userList.get(i).getDepartment());
        }
        try {
            jsonRes.put("scoreList", scoreList);
            jsonRes.put("nameList", nameList);
            jsonRes.put("dateList", dateList);
            jsonRes.put("departmentList", departmentList);
            jsonRes.put("idList", idList);            
            writer.write(jsonRes.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
